import Vue from 'vue';
import VueRouter from 'vue-router';
// import VueResource from 'vue-resource';
import App from './App';
import 'lib-flexible';
import 'weui';
import FastClick from 'fastclick';

// vuex
import {
  sync
}
from 'vuex-router-sync';
import store from './vuex/store';

import {
  Date
}
from "./lib/utils";

//调试
// import "vconsole";


//views
import InventoryList from './views/inventory-list';

//business
import Inventories from './business/inventories';


require('es6-promise').polyfill();


Vue.config.debug = true;

Vue.use(VueRouter);
Vue.use(require('vue-resource'));
Vue.use(require('vue-gesture'));


Vue.filter('wrap', function(value, id, cart) {
  var qty = 0;
  cart.items.forEach(function(ele, i) {
    if (ele.inventoryId == id) {
      qty += ele.qty
    }
  });
  return qty;
});

Vue.filter('formatDate', function(value) {
  // `input` === `this.userInput`
  return Date.formatDate(value)
})

Vue.directive('inventory-qty-valid', {
  params: ['inventory'],
  bind: function() {
    if (Inventories.isInventoryIsRequired(this.params.inventory)) {
      this.el.classList.add("require");
    }
  },
  update: function(value) {
    let _classList = this.el.classList;
    if (value > 1) {
      _classList.add("icon-inventory-subtract");
      _classList.remove("icon-inventory-delete");
      _classList.add("disabled");
    } else if (value == 1) {
      _classList.remove("icon-inventory-subtract");
      _classList.remove("disabled");
      _classList.add("icon-inventory-delete");
    }
  }
});
Vue.directive('inventory-qty-show', function(value) {
  // console.log(value);
  if (!value) {
    this.el.classList.add("dn");
  } else {
    this.el.classList.remove("dn");
  }
});
Vue.directive('inventory-options-required', function(value) {
  if (Inventories.isInventoryIsRequired(value)) {
    this.el.classList.add("require");
  }
});

// debugger
const router = new VueRouter();
router.map({
  '/': {
    name: "inventory-list",
    component: InventoryList,
    subRoutes: {
      'inventory-info': {
        name: "inventory-info",
        component: require('./views/inventory-info.vue')
      },
      'inventory-option-group': {
        name: "inventory-option-group",
        component: require('./views/inventory-option-group')
      },
      'confirm-order': {
        name: "confirm-order",
        component: require('./views/confirm-order.vue')
      },
      'complete-order': {
        name: "complete-order",
        component: require('./views/complete-order.vue')
      },
      'select-pay': {
        name: "select-pay",
        component: require('./views/select-pay.vue')
      },
      'show-plate': {
        name: "show-plate",
        component: require('./views/show-plate.vue')
      },
      'order-detail': {
        name: "order-detail",
        component: require('./views/order-detail.vue')
      },
      'order-list': {
        name: "order-list",
        component: require('./views/order-list.vue')
      },
      'start-page': {
        name: "start-page",
        component: require('./views/start-page.vue')
      }
    }
  }
});
sync(store, router);
router.start(App, 'app');

FastClick.attach(document.body);
